package com.yami.shop.service.impl;


import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.app.param.UserXsCardParam;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserXsCard;
import com.yami.shop.bean.param.hnaPay.BaseTransForm;
import com.yami.shop.config.HnaPayConfig;
import com.yami.shop.dao.UserXsCardMapper;
import com.yami.shop.service.UserService;
import com.yami.shop.service.UserXsCardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author lgh on 2018/09/11.
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserXsCardServiceImpl extends ServiceImpl<UserXsCardMapper, UserXsCard> implements UserXsCardService {
    private final UserXsCardMapper userXsCardMapper;

    private final HnaPayConfig hnaPayConfig;

    private final UserService userService;


    @Override
    public List<UserXsCard> getUserCardsByUserIdOrXsUserId(String userId, String xsUserId) {
        return userXsCardMapper.getUserCardsByUserIdOrXsUserId(userId, xsUserId);
    }

    @Override
    public UserXsCardParam addCard(UserXsCardParam userXsCardParam,String userId) {
        User byUserId = userService.getUserByUserId(userId);
        UserXsCard userXsCard = new UserXsCard();
        userXsCard.setUserId(userId);
        userXsCard.setXsUserId(userXsCardParam.getXsUserId());
        userXsCard.setIdentityCode(byUserId.getIdNumber());
        userXsCard.setIdentityType("1");
        userXsCard.setCardNo(userXsCardParam.getCardNo());
        userXsCard.setHolderName(byUserId.getRealName());
        userXsCard.setMobileNo(userXsCardParam.getMobileNo());
        userXsCard.setId(IdUtil.getSnowflakeNextId());
        userXsCard.setStatus("0");
        userXsCard.setCreateTime(new Date());
        userXsCard.setUpdateTime(new Date());
        BaseTransForm base = hnaPayConfig.getR007TransForm(userXsCard);
        try {
            Map<String, Object> responseMap = hnaPayConfig.post(base);
            log.info("R007TransForm res{}",JSONUtil.toJsonStr(responseMap));
            if (responseMap.get("resultCode").toString().equals("0000")) {
                int insert = userXsCardMapper.insert(userXsCard);
                if (insert > 0) {
                    String ncountOrderId = responseMap.get("ncountOrderId").toString();
                    userXsCardParam.setId(userXsCard.getId().toString());
                    userXsCardParam.setNcountOrderId(ncountOrderId);
                    return userXsCardParam;
                }
            }
        } catch (Exception e) {
            log.error("Exception", e);
            log.error("addCard error{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return null;
    }

    @Override
    public boolean addCardConfirm(UserXsCardParam userXsCardParam) {
        UserXsCard userXsCard = userXsCardMapper.selectById(Long.valueOf(userXsCardParam.getId()));
        if(userXsCard.getStatus().equals("1")){
            return false;
        }
        BaseTransForm base = hnaPayConfig.getR008TransForm(userXsCardParam.getNcountOrderId(), userXsCardParam.getSmsCode());
        try {
            Map<String, Object> responseMap = hnaPayConfig.post(base);
            log.info("R008TransForm res{}",JSONUtil.toJsonStr(responseMap));
            if (responseMap.get("resultCode").toString().equals("0000")) {
                userXsCard.setStatus("1");
                userXsCard.setBindCardAgrNo(responseMap.get("bindCardAgrNo").toString());
                userXsCard.setBankCode(responseMap.get("bankCode").toString());
                userXsCard.setCardType(responseMap.get("cardType").toString());
                userXsCard.setShortCardNo(responseMap.get("shortCardNo").toString());
                userXsCard.setUpdateTime(new Date());
                int i = userXsCardMapper.updateById(userXsCard);
                if (i > 0) {
                    return true;
                }
            }
        } catch (Exception c) {
            log.error("addCardConfirm error{}", c.getMessage());
            throw new RuntimeException(c);
        }
        return false;
    }

    @Override
    public UserXsCard getUserCardById(Long id) {
        return userXsCardMapper.selectById(id);
    }

    @Override
    public boolean updateCardStatus(Long id, String status) {
        UserXsCard userXsCard = userXsCardMapper.selectById(id);
        BaseTransForm base = hnaPayConfig.getR009TransForm(userXsCard.getXsUserId(), userXsCard.getBindCardAgrNo());
        try {
            Map<String, Object> responseMap = hnaPayConfig.post(base);
            if (responseMap.get("resultCode").toString().equals("0000")) {
                int i = userXsCardMapper.updateStatusById(id, status);
                if (i > 0) {
                    return true;
                }
            }
        } catch (Exception e) {
            log.error("updateCardStatus error{}", e.getMessage());
            throw new RuntimeException(e);
        }
        return false;
    }
}
